package com.inviteOrder.strategy;

import com.inviteOrder.dao.User;
import com.inviteOrder.domain.Reward;
import java.util.HashMap;
import java.util.Map;

/**
 * 返奖上下文
 * @author 大忽悠
 * @create 2022/10/17 10:12
 */
public class RewardStrategyContext {
    public static final String NEW_USER="NEW_USER";
    public static final String OLD_USER="OLD_USER";
    /**
     * 返奖策略集合
     */
    private static Map<String,RewardStrategy> rewardStrategies=new HashMap<>();

    static {
        rewardStrategies.put(NEW_USER,new NewUserRewardStrategy());
        rewardStrategies.put(OLD_USER,new OldUserRewardStrategy());
    }

    public Boolean doReward(Long uid){
        try {
            User user = User.getUser(uid);
            RewardStrategy rewardStrategy = rewardStrategies.get(user.getType());
            checkRewardStrategy(user.getType(),rewardStrategy);
            Reward reward = rewardStrategy.reward(uid);
            User.getReward(reward,uid);
        }catch (Throwable throwable){
            throwable.printStackTrace();
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    private void checkRewardStrategy(String strategy, RewardStrategy rewardStrategy) {
       if(rewardStrategy==null){
           throw new IllegalArgumentException("不存在名为"+strategy+"的策略");
       }
    }
}
